package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsCustomDictionary;
import com.jy.wms.service.WmsCustomDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsCustomDictionaryController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Dec 26 13:58:33 CST 2017
 */
@Controller
@RequestMapping("/wmscustomdictionary")
@Api(value = "/wmscustomdictionary", description = "自定义状态")
public class  WmsCustomDictionaryController extends BaseController<WmsCustomDictionaryService,WmsCustomDictionary> {

    @PostMapping(value = {"list"})
    @ApiOperation(value = "查询信息",response = JSONObject.class, notes = "查询信息")
    public @ResponseBody
    ResultPage<WmsCustomDictionary> getWmsCustomDictionary(@RequestBody WmsCustomDictionary entity, HttpSession session) {
        Page<WmsCustomDictionary> list= baseService.baseSelectPage(entity);
            return renderPage(list);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除信息",response = JSONObject.class, notes = "删除信息")
    public @ResponseBody
    JsonResult deleteWmsCustomDictionary(@RequestBody List<String> ids) {
        Integer flag=baseService.baseDeleteBathPrimaryKeys(ids);
        return renderSuccess("删除成功");
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增自定义状态信息",response = JSONObject.class, notes = "新增自定义状态信息")
    public @ResponseBody JSONObject addWmsCustomDictionary(@RequestBody WmsCustomDictionary entity, HttpSession session) {
        try{
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//获取仓库Id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid=hz.get(0).getHzid();//获取货主Id
            entity.setId(UUIDUtils.uuid());
            baseService.baseInsert(entity,sshzid,ckid);//新增状态
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增自定义状态信息成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增自定义状态信息失败", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增自定义状态信息失败", e.getMessage(), "");
        }
    }

    @PostMapping(value = "getWmsCustomDictionary/{zcolumn}")
    @ApiOperation(value = "查询当前货主下的单个列的状态",response = JSONObject.class, notes = "查询当前货主下的单个列的状态")
    public @ResponseBody JSONObject getWmsCustomDictionaryById(@PathVariable("zcolumn") String zcolumn, HttpSession session) {
        JSONObject json=new JSONObject();
        try{
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//获取仓库Id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid=hz.get(0).getHzid();//获取货主Id
            List<WmsCustomDictionary> list=baseService.baseSelectByPrimaryKey(sshzid,ckid,zcolumn);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑自定义状态信息",response = JSONObject.class, notes = "编辑自定义状态信息")
    public @ResponseBody JSONObject editWmsCustomDictionary(@RequestBody WmsCustomDictionary entity, HttpSession session) {
        try{
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//获取仓库Id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid=hz.get(0).getHzid();//获取货主Id
            entity.setSshzid(sshzid);
            entity.setCkid(ckid);
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"编辑自定义状态信息成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑自定义状态信息失败", e.getMessage(), "");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑自定义状态信息失败", e.getMessage(), "");
        }
    }
}
